﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MongoDB.Bson;
using Chaine.DAO;
using Chaine.Models;
using System.IO;
using System.Text.RegularExpressions;

namespace Chaine.Controllers
{
    public class AdminPhotoController : Controller
    {
        //
        // GET: /AdminPhoto/

        public ActionResult Index(string eventId)
        {
            var e = EventManager.GetEvent(ObjectId.Parse(eventId));
            return View("~/views/admin/photo/index.cshtml", e);
        }

        public ActionResult Upload(string eventid)
        {
            var e = EventManager.GetEvent(ObjectId.Parse(eventid));
            return View("~/views/admin/photo/upload.cshtml", e);
        }

        [HttpPost]
        public ActionResult Upload(string eventid, FormCollection form)
        {
            var e = EventManager.GetEvent(ObjectId.Parse(eventid));
            for (int i = 0; i < Request.Files.Count; i++)
            {
                var file = Request.Files[i] as HttpPostedFileWrapper;
                //判断文件的类型
                if (file != null && file.ContentLength > 0)
                {
                    var isMatch = Regex.IsMatch(file.FileName.ToLower(), @"\.(jpg|jpeg|png)$");
                    if (!isMatch)
                    {
                        ModelState.AddModelError("files", "");
                        break;
                    }
                }
            }
            if (!ModelState.IsValid)
                return View("~/views/admin/photo/upload.cshtml", e);
            if (ModelState.IsValid)
            {
                //上传照片
                for (int i = 0; i < Request.Files.Count; i++)
                {
                    var file = Request.Files[i] as HttpPostedFileWrapper;
                    PhotoManager.UploadPhotos(ObjectId.Parse(eventid), file);
                }
            }

            TempData["upload-success"] = "";
            return RedirectToAction("Index", new { eventid = eventid });
        }

        //
        // POST: /AdminPhoto/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminPhoto/Edit/5

        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /AdminPhoto/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /AdminPhoto/Delete/5

        public ActionResult Delete(string eventid, string filename)
        {
            var e = EventManager.GetEvent(ObjectId.Parse(eventid));
            return View("~/views/admin/photo/delete.cshtml", e);
        }

        //
        // POST: /AdminPhoto/Delete/5

        [HttpPost]
        public ActionResult Delete(EventModels e, string eventid, string filename, FormCollection collection)
        {
            try
            {
                var result = PhotoManager.DeletePhoto(ObjectId.Parse(eventid), filename + ".jpg");
                if (!result)
                {
                    Response.Write("false");
                   //return RedirectToAction("Delete", new { eventid=eventid,filename=filename});
                }
                return RedirectToAction("Index", new { eventid = eventid });
            }
            catch(Exception ex)
            {
                throw ex;
                //return RedirectToAction("Delete", new { eventid = eventid, filename = filename });
            }
        }
    }
}
